package com.foreknow.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constantitem;
import com.foreknow.entity.Constanttype;
import com.foreknow.service.ConstantitemService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/Constantitem")
public class ConstantitemController {
    @Autowired
    private ConstantitemService constantitemService;
    @RequestMapping(value = "/findConstantitemByPage",method = RequestMethod.POST)
    @ResponseBody
    public String findConstantitemByPage(@RequestBody Map<String,Object> param) throws JsonProcessingException {
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        String query = (String) param.get("query");
        List<Constantitem> list = constantitemService.searchAll(query);
        HashMap<String,Object> hs = new HashMap<>();
        if (list != null){
            PageInfo<Constantitem> info = new PageInfo<>(list);
            hs.put("data",info);
            hs.put("meta",new Constance("获取常数列表成功",200));
        }else{
            hs.put("meta",new Constance("获取常数列表失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findConstantitemByid",method = RequestMethod.POST)
    @ResponseBody
    public String findConstantitemByid(@RequestBody Map<String,Object> param) throws JsonProcessingException {
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        Integer id = (Integer) param.get("id");
        List<Constantitem> list1 =  constantitemService.selectById(id);
        HashMap<String,Object> hs = new HashMap<>();
        if (list1 != null){
            PageInfo<Constantitem> info = new PageInfo<>(list1);
            hs.put("data",info);
            hs.put("meta",new Constance("获取常数类别详情成功",200));
        }else {
            hs.put("meta",new Constance("获取常数类别详情失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/addConstantitem", method = RequestMethod.POST)
    @ResponseBody
    public String addConstantitem(@RequestBody  Constantitem constantitem) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int a =constantitemService.Insert(constantitem);
        if (a==1){
            hs.put("meta",new Constance("添加成功",200));
        }else {
            hs.put("meta",new Constance("添加失败",401));
        }
        ObjectMapper objectMapper =  new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/delectConstantitem",method = RequestMethod.GET)
    public String delectConstantitem(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String,Object> hs =new HashMap<>();
         int a = constantitemService.Delete(id);
         if (a==1){
             hs.put("meta",new Constance("删除成功",200));
         }else {
             hs.put("meta",new Constance("删除失败",401));
         }
         ObjectMapper objectMapper =new ObjectMapper();
         return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/updatetConstantitem",method = RequestMethod.POST)
    @ResponseBody
    public String updatetConstantitem(@RequestBody Constantitem constantitem) throws JsonProcessingException {
        HashMap<String,Object> hs =new HashMap<>();
        int a =  constantitemService.update(constantitem);
      if (a==1){
          hs.put("meta",new Constance("修改成功",200));
      }else{
          hs.put("meta",new Constance("修改失败",400));
      }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/selectConstantitem",method = RequestMethod.GET)
    public String selectConstantitem(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String,Object> hs =new HashMap<>();
        Constantitem constantitem = constantitemService.selectByPrimaryKey(id);
        if (constantitem!=null){
            hs.put("data",constantitem);
            hs.put("meta",new Constance("查询",200));
        }else {
            hs.put("meta",new Constance("删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}